package com.dangs.color;

import android.app.Service;
import android.app.SystemUtilsManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;

import com.dangbei.os.manager.platform.DBPlatformConstants;
import com.dangbei.os.manager.platform.DBPlatformManager;
import com.dangbei.os.manager.system.DBSystemManager;
import com.mstar.android.tv.TvAudioManager;
import com.mstar.android.tv.TvCecManager;
import com.mstar.android.tv.TvS3DManager;
import com.mstar.android.tvapi.common.vo.CecSetting;

import java.io.FileWriter;
import java.io.IOException;

public class BackService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("ytb", "Service onCreate");
        IntentFilter intentFilter = new IntentFilter("cmd_i");
        intentFilter.addAction("cmd_s");
        intentFilter.addAction("cmd_f");
        intentFilter.addAction("3d");
        registerReceiver(receiver, intentFilter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("ytb", "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("ytb", "onDestroy");
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String key = intent.getStringExtra("key");
//            String value_s = intent.getStringExtra("value");
            int value_i = intent.getIntExtra("value", 0);
//            float value_f = intent.getFloatExtra("value", 0.0f);
//            Log.d("ytb", "action:" + action + ", key:" + key + ", value_i:" + value_i + ", value_f:" + ", value_s:" + value_s);
            Log.d("ytb", "action:" + action + ", key:" + key + ", value_i:" + value_i);
            int ret = 0;
            if ("3d".equals(action)) {
                int mode = intent.getIntExtra("mode", 0);
                DBPlatformManager.getInstance().switchDimension(mode);
            }
            if (TextUtils.isEmpty(key)) {
                return;
            }
            switch (key) {
                case "mute":
//                    ret = TvAudioManager.getInstance().enableAudioMute(value_i);
                    break;
                case "unmute":
//                    ret = TvAudioManager.getInstance().disableAudioMute(value_i);
                    break;
                case "mode":
//                    TvAudioManager.getInstance().setAudioSpdifOutMode(value_i);
                    break;
                case "cmd_f":
//                    ret = TvAudioManager.getInstance().enableAudioMute(value_i);
                    Log.d("ytb", "enableAudioMute:"  + ", ret:" + ret);
//                    ret = TvAudioManager.getInstance().disableAudioMute(value_i);
                    Log.d("ytb", "disableAudioMute:"  + ", ret:" + ret);
                    break;
                case "cmd_i":
                    write_mode("/sys/class/amp/amplifler/enpower","0");
//                    CecSetting mCecSetting;
//                    mCecSetting = TvCecManager.getInstance().getCecConfiguration();
//                    mCecSetting.arcStatus = 0;
//                    TvCecManager.getInstance().setCecConfiguration(mCecSetting);
//                    ret = TvAudioManager.getInstance().enableAudioMute(value_i);
//                    Log.d("ytb", "enableAudioMute spdif:"  + ", ret:" + ret);
//                    ret = TvAudioManager.getInstance().disableAudioMute(value_i);
//                    Log.d("ytb", "disableAudioMute spdif:"  + ", ret:" + ret);
//                    ret = TvAudioManager.getInstance().enableAudioMute(TvAudioManager.AUDIO_MUTE_USER_SPEAKER);
//                    Log.d("ytb", "enableAudioMute speaker:"  + ", ret:" + ret);
//                    TvAudioManager.getInstance().enableAudioMute(TvAudioManager.AUDIO_MUTE_USER_HP);
//                    Log.d("ytb", "enableAudioMute hp:"  + ", ret:" + ret);
                    break;
                case "cmd_spdif":
                    SystemUtilsManager.getInstance().setAudioSpdifOutMode("pcm");
                    break;
            }
            Log.d("ytb", "action:" + action + ", ret:" + ret);
        }
    };

    private void write_mode(String path, String str){
        try {
            FileWriter fileWritter = new FileWriter(path,true);
            fileWritter.write(str);
            fileWritter.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }


}
